package learn_effective_java.item_28;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Chooser<T> {
	private final List<T> choiceArray;
	
	public Chooser(Collection<T> choices) {
		this.choiceArray = new ArrayList<>(choices);
	}
	
	public T choose() {
		Random rnd = ThreadLocalRandom.current();
		return choiceArray.get(rnd.nextInt(choiceArray.size()));
	}
	
	public static void main(String[] args) {
		List<Integer> intList = Arrays.asList(
											Integer.valueOf(1), 
											Integer.valueOf(2),
											Integer.valueOf(5),
											Integer.valueOf(3));
		Chooser<Integer> chooser = new Chooser<>(intList);
		Integer e = chooser.choose();
		System.out.println(e);
	}	
}
